﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCBugTracker.Areas.UserManagement.Models;
using System.Web.Routing;
using MVCBugTracker.Areas.UserManagement.ViewModels;

namespace MVCBugTracker.Areas.UserManagement.Controllers
{
    public class UserController : Controller
    {

        public IMembershipService MembershipService { get; set; }
        public IRoleService RoleService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (MembershipService == null) { MembershipService = new UserMembershipService(); }
            if (RoleService == null) { RoleService = new RoleService(); }

            base.Initialize(requestContext);
        }

        // GET: /UserManagement/User/
        public ActionResult Index()
        {
            var results = new UserIndexViewModel()
            {
                PageIndex = 0,
                PageSize = 1
            };
            int totalUsers;
            results.Users = MembershipService.GetAllUsers(0, 100, out totalUsers);
            results.TotalRecorders = totalUsers;
            return View(results);
        }

        // GET: /UserManagement/User/Create
        public ActionResult Create()
        {
            return View();
        } 

        // POST: /UserManagement/User/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        // GET: /UserManagement/User/Edit/5
        public ActionResult Edit(string userName)
        {
            return View(MembershipService.GetUser(userName));
        }

        //
        // POST: /UserManagement/User/Edit/5

        [HttpPost]
        public ActionResult Edit(string userName, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /UserManagement/User/Delete/5
 
        public ActionResult Delete(string userName)
        {
            return View();
        }

        //
        // POST: /UserManagement/User/Delete/5

        [HttpPost]
        public ActionResult Delete(string userName, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
